package com.yckj.activechicken.business.common.service.impl;

import com.yckj.activechicken.business.common.mapper.CustomSequenceMapper;
import com.yckj.activechicken.business.common.model.CustomSequence;
import com.yckj.activechicken.business.common.model.CustomSequenceExample;
import com.yckj.activechicken.business.common.service.ISequenceService;
import com.yckj.activechicken.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.common.service.impl
 * </p>
 * <p/>
 * <p>
 * Description: 描述【系统序列管理业务处理实体】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/6/7 21:08
 */
@Service
public class SequenceServiceImpl implements ISequenceService {
    @Autowired
    private CustomSequenceMapper customSequenceMapper;

    /**
     * 添加序列
     * @param customSequence
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String,Object> addSequence(CustomSequence customSequence) {
        Map<String,Object> outMap = new HashMap<>(2);

        //校验序列是否存在
        CustomSequenceExample sequenceExample = new CustomSequenceExample();
        sequenceExample.createCriteria().andNameEqualTo(customSequence.getName());
        List<CustomSequence> list = customSequenceMapper.selectByExample(sequenceExample);
        if(StringUtils.isNotEmpty(list)){
            outMap.put("result","false");
            outMap.put("message","该序列名称已存在，请重新输入！");
            return outMap;
        }

        //校验前缀是否存在
        if(StringUtils.isNotEmpty(customSequence.getPrefixString())){
            sequenceExample.clear();
            sequenceExample.createCriteria().andPrefixStringEqualTo(customSequence.getPrefixString());
            List<CustomSequence> list1 = customSequenceMapper.selectByExample(sequenceExample);
            if(StringUtils.isNotEmpty(list1)){
                outMap.put("result","false");
                outMap.put("message","该序列前缀已存在，请重新输入！");
                return outMap;
            }
        }else{
            customSequence.setPrefixString("");
        }


        int num = customSequenceMapper.insertSelective(customSequence);
        customSequenceMapper.SetVal(customSequence);

        if(num > 0){
            outMap.put("result","true");
            return outMap;
        }else{
            outMap.put("result","false");
            outMap.put("message","序列添加失败！");
            return outMap;
        }
    }

    @Override
    public int modifySequence(CustomSequence customSequence) {
        int num = customSequenceMapper.updateByPrimaryKeySelective(customSequence);
        return num;
    }

    /**
     * 分页查询序列信息
     * @param customSequence
     * @return
     */
    @Override
    public List<CustomSequence> queryList(CustomSequence customSequence) {
        return customSequenceMapper.selectSequenceList(customSequence);
    }
}
